import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/education/train_plan/custom_evalute_input_page.dart';
import 'package:enterprise_v6_app/app/components/education/train_plan/focus_custom_evalute_input_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/utils/user_manager.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/education_comment_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/get_children_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/course_detail_detail_page/course_detail/course_detail_detail_page/evaluate_detail/evaluate_detail_controller.dart';

///人脸识别后的课程详情-》评价
class EvaluateDetailPage extends StatelessWidget {
  EvaluateDetailPage({super.key});

  final controller = Get.put(EvaluateDetailController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Stack(
            children: [
              _topWidget(),
              _bottomWidget(context),
            ],
          );
        });
  }

  Widget _rowData(EducationCommentListEntity e, String name, String time, String value, String commentLike, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontWeight: FontWeight.w400,
            fontSize: 14.sp,
          ),
        ),
        AppGaps.hGap8,
        Text(
          time,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SvgPicture.asset(AppImages.icon_train_detail_questions),
                  AppGaps.hGap4,
                  Text(
                    value,
                    style: TextStyle(
                      color: const Color(0xFFA0A7B5),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  )
                ],
              ),
              AppGaps.hGap12,
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SvgPicture.asset(AppImages.icon_course_eye),
                  AppGaps.hGap4,
                  Text(
                    commentLike,
                    style: TextStyle(
                      color: const Color(0xFFA0A7B5),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      ],
    );
  }

  Widget itemWidget(EducationCommentListEntity item) {
    return Container(
      margin: EdgeInsets.all(14.w),
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: const Color(0x1AA5A9C8),
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Text(
                  item.commentContent ?? '',
                  style: TextStyle(
                    color: const Color(0xFF495061),
                    fontWeight: FontWeight.w400,
                    fontSize: 14.sp,
                  ),
                ),
              ),
              // 只删除自己的
              Visibility(
                visible: UserManager.getUserId() == item.commentUserId,
                child: SvgPicture.asset(AppImages.icon_train_questions_answers).clickOnce(() {
                  //删除
                  controller.educationCommentDel(item.id);
                }),
              ),
            ],
          ),
          AppGaps.vGap6,
          _rowData(item, item.commentUserName ?? '', item.createDate ?? '', '${item.commentCount ?? '0'}', '${item.commentLike ?? '0'}',
              alignment: Alignment.centerRight),
        ],
      ),
    ).clickOnce(() {
      itemDetail(item);
    });
  }

  Widget itemWidgetDialog(GetChildrenChildren item, int index, BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Obx(() => Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: EdgeInsets.only(left: 14.w, right: 14.w, top: 14.w, bottom: 14.w),
                color: item.isShow.value ? const Color(0x1AA5A9C8) : Colors.white,
                child: Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          child: Text(
                            item.commentContent ?? '',
                            style: TextStyle(
                              color: const Color(0xFF495061),
                              fontWeight: FontWeight.w400,
                              fontSize: 14.sp,
                            ),
                          ),
                        ),
                      ],
                    ),
                    AppGaps.vGap6,
                    Row(
                      children: [
                        Text(
                          item.commentUserName ?? '',
                          style: TextStyle(
                            color: const Color(0xFF495061),
                            fontWeight: FontWeight.w400,
                            fontSize: 14.sp,
                          ),
                        ),
                        AppGaps.hGap8,
                        Text(
                          '${item.createDate ?? ''}发表',
                          style: TextStyle(
                            color: const Color(0xFFA5A6B5),
                            fontWeight: FontWeight.w400,
                            fontSize: 12.sp,
                          ),
                        ),
                        Expanded(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Row(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  SvgPicture.asset(AppImages.icon_train_detail_questions),
                                  AppGaps.hGap4,
                                  Text(
                                    '${item.commentCount ?? '0'}',
                                    style: TextStyle(
                                      color: const Color(0xFFA0A7B5),
                                      fontSize: 12.sp,
                                      fontWeight: FontWeight.w400,
                                    ),
                                  )
                                ],
                              ),
                              AppGaps.hGap12,
                              Obx(() => Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      SvgPicture.asset(AppImages.icon_train_like_),
                                      AppGaps.hGap4,
                                      Text(
                                        '${item.commentLikeX?.value ?? '0'}',
                                        // '${item.commentLike ?? '0'}',
                                        style: TextStyle(
                                          color: const Color(0xFFA0A7B5),
                                          fontSize: 12.sp,
                                          fontWeight: FontWeight.w400,
                                        ),
                                      )
                                    ],
                                  ).clickOnce(() {
                                    controller.onClickGetChildrenChildren(item);
                                  })),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ).clickOnce(() {
                if (controller.tobGetChildrenChildren != null && controller.tobGetChildrenChildren != item) {
                  //处理上一个条目背景 条目背景逻辑
                  controller.tobGetChildrenChildren?.isShow.value = false;
                }
                //条目展开逻辑 条目背景逻辑
                // item.isShow.value = !item.isShow.value;
                item.isShow.toggle();
                //设置当前点击的id
                controller.onclickId = item.id;
                controller.tobGetChildrenChildren = item;

                ///设置id
                controller.clickPullName.value = item.commentUserName ?? '';
                controller.clickPullNameParentID = item.commentUserId ?? '';
                // controller.clickPullNamePidParentID = item.commentPid ?? '';
                controller.clickPullNamePidParentID = item.id ?? '';
                if (item.isShow.value) {
                  controller.callKeyboardShow();
                  FocusScope.of(context).requestFocus(controller.focusNode);
                  //而FocusManager.instance.primaryFocus?.requestFocus(controller.focusNode) 请求的焦点是全局性的，可能会导致整个应用的焦点状态发生变化，从而影响ListView的滑动。
                  //如果第二个方法在使用ListView的animateTo时发生了问题，可能是因为请求焦点导致了整个应用的渲染，从而影响了ListView的滑动效果。在处理焦点时，最好在局部范围内使用FocusScope以限制影响范围，从而避免全局性的影响。
                  //第二个方法: FocusManager.instance.primaryFocus?.requestFocus(controller.focusNode);
                  //滑动到指定点击位置
                  controller.globalKeyDialog.currentState?.getController()?.animateTo(
                        index * 75.w, // 每个条目的高度
                        duration: const Duration(milliseconds: 300),
                        curve: Curves.easeInOut,
                      );
                } else {
                  FocusManager.instance.primaryFocus?.unfocus();
                  // controller.callKeyboardHide();
                }
                //如果都没有点击 就是最外层（赋值给最外层）
                if (controller.tobGetChildrenChildren != null && !item.isShow.value && controller.tobGetChildrenChildren == item) {
                  //设置id
                  controller.clickPullName.value = controller.clickPullNameP;
                  controller.clickPullNameParentID = controller.clickPullNameParentIDP;
                  // controller.clickPullNamePidParentID = item.commentPid ?? '';
                  controller.clickPullNamePidParentID = controller.clickPullNamePidParentIDP;

                  //设置当前点击的id 要置空
                  controller.onclickId = '';
                }
              }),
              //隐藏的内容；
              Visibility(
                visible: item.isShow.value,
                child: Column(
                  children: item.children!.map(_dialogClickC).toList(),
                ),
              )
            ],
          )),
    );
  }

  //搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    focusNode: controller.focusNode2,
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  void itemDetail(EducationCommentListEntity item) {
    controller.firstLevelCommentId = item.id ?? ''; //点击的id
    controller.first.value = !controller.first.value;

    //对弹出界面底部的回复名字
    controller.clickPullName.value = item.commentUserName ?? '';
    controller.clickPullNameParentID = item.commentUserId ?? '';
    controller.clickPullNamePidParentID = item.id ?? '';

    //弹出不变的id和名字
    controller.clickPullNameP = item.commentUserName ?? '';
    controller.clickPullNameParentIDP = item.commentUserId ?? '';
    controller.clickPullNamePidParentIDP = item.id ?? '';
  }

  ///弹框点击后的
  Widget _dialogClickC(GetChildrenChildrenChildren getChildrenChildrenChildren) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppGaps.vGap2,
        Row(
          children: [
            AppGaps.hGap64,
            Text(
              getChildrenChildrenChildren.commentUserName ?? '',
              style: TextStyle(
                color: const Color(0xFF495061),
                fontWeight: FontWeight.w400,
                fontSize: 14.sp,
              ),
            ),
            AppGaps.hGap8,
            Text(
              // '2天前回复',
              '${getChildrenChildrenChildren.createDate ?? ''}前回复',
              style: TextStyle(
                color: const Color(0xFFA0A7B5),
                fontWeight: FontWeight.w400,
                fontSize: 12.sp,
              ),
            ),
          ],
        ),
        AppGaps.vGap8,
        Row(
          children: [
            AppGaps.hGap64,
            Text(
              getChildrenChildrenChildren.commentContent ?? '',
              style: TextStyle(
                color: const Color(0xFF495061),
                fontWeight: FontWeight.w400,
                fontSize: 14.sp,
              ),
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Obx(() => Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          SvgPicture.asset(AppImages.icon_train_like_),
                          AppGaps.hGap4,
                          Text(
                            '${getChildrenChildrenChildren.commentLikeX?.value ?? '0'}',
                            style: TextStyle(
                              color: const Color(0xFFA0A7B5),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                            ),
                          )
                        ],
                      ).clickOnce(() {
                        controller.onClickGetChildrenChildrenChildren(getChildrenChildrenChildren);
                      }))
                ],
              ),
            ),
            AppGaps.hGap14,
          ],
        ),
        AppGaps.vGap12,
      ],
    );
  }

  Widget _bottomWidget(BuildContext context) {
    return Obx(() => Visibility(
        visible: controller.first.value,
        child: Scaffold(
          body: Stack(
            children: [
              Container(
                color: Colors.white,
                padding: EdgeInsets.only(top: 12.w, bottom: 115.w),
                child: Column(
                  children: [
                    Expanded(
                      child: Column(
                        children: [
                          Container(
                            margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 16.w),
                            padding: EdgeInsets.all(12.w),
                            decoration: BoxDecoration(
                              color: const Color(0x1AA5A9C8),
                              borderRadius: BorderRadius.circular(8.w),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Text(
                                        controller.getChildrenEntity?.commentContent ?? '',
                                        style: TextStyle(
                                          color: const Color(0xFF495061),
                                          fontWeight: FontWeight.w400,
                                          fontSize: 14.sp,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                AppGaps.vGap6,
                                Row(
                                  children: [
                                    Text(
                                      controller.getChildrenEntity?.commentUserName ?? '',
                                      style: TextStyle(
                                        color: const Color(0xFF495061),
                                        fontWeight: FontWeight.w400,
                                        fontSize: 14.sp,
                                      ),
                                    ),
                                    AppGaps.hGap8,
                                    Text(
                                      '${controller.getChildrenEntity?.createDate}发表',
                                      style: TextStyle(
                                        color: const Color(0xFFA5A6B5),
                                        fontWeight: FontWeight.w400,
                                        fontSize: 12.sp,
                                      ),
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ).clickOnce(() {
                            controller.clickPullName.value = controller.clickPullNameP;
                            controller.clickPullNameParentID = controller.clickPullNameParentIDP;
                            controller.clickPullNamePidParentID = controller.clickPullNamePidParentIDP;
                            //设置当前点击的id 要置空
                            controller.onclickId = '';
                            //点击最顶上的问题
                            controller.globalKeyDialog.currentState?.onRefresh();
                            FocusManager.instance.primaryFocus?.unfocus();
                          }),
                          AppGaps.vGap16,
                          Row(
                            children: [
                              AppGaps.hGap14,
                              Text(
                                '共${controller.listGetChildrenChildren?.length}条回复',
                                style: TextStyle(
                                  color: const Color(0xFF5C6DFF),
                                  fontWeight: FontWeight.w400,
                                  fontSize: 16.sp,
                                ),
                              ),
                              Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        Text(
                                          '按时间排序',
                                          style: TextStyle(
                                            color: const Color(0xFFA0A7B5),
                                            fontSize: 14.sp,
                                            fontWeight: FontWeight.w400,
                                          ),
                                        ),
                                        AppGaps.hGap4,
                                        SvgPicture.asset(AppImages.icon_train_sort_by_time),
                                        AppGaps.hGap14,
                                      ],
                                    ).clickOnce(() {
                                      controller.order = !controller.order;
                                      controller.globalKeyDialog.currentState?.onRefresh();
                                    })
                                  ],
                                ),
                              ),
                            ],
                          ),
                          AppGaps.vGap10,
                          Padding(
                            padding: EdgeInsets.only(left: 16.w, right: 15.w),
                            child: Container(
                              color: const Color(0xFFA5A9C8).withOpacity(0.2.w),
                              height: 1.w,
                            ),
                          ),
                          Expanded(
                              child: CustomEasyRefreshList<GetChildrenChildren>(
                            key: controller.globalKeyDialog,
                            enableLoad: false,
                            dataLoad: controller.getChildrenE,
                            itemBuilder: (item, index) {
                              return itemWidgetDialog(item, index, context);
                            },
                            separated: Container(
                              color: const Color(0xFFA5A9C8).withOpacity(0.2),
                              height: 1.w,
                            ),
                          )),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Positioned(
                right: 0,
                top: 5,
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(17.w),
                    color: Colors.blue,
                  ),
                  height: 24.w,
                  width: 24.w,
                  child: Icon(
                    Icons.close,
                    size: 18.w,
                    color: const Color(0xFF262F38),
                  ),
                ).clickOnce(() {
                  controller.first.value = !controller.first.value;
                  //要置空 要不弹出来的界面会显示展开的效果
                  controller.onclickId = '';
                }),
              ),
            ],
          ), // 右侧筛选
          bottomSheet: Obx(() => Container(
                height: 129.w,
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xffBCBCC5).withOpacity(0.2), // 阴影颜色
                      offset: const Offset(0, -2), // 阴影的偏移量，向上偏移，即上方阴影
                      blurRadius: 4.0, // 阴影的模糊半径
                      spreadRadius: 1.0, // 阴影的扩散半径
                    ),
                  ],
                ),
                child: Column(
                  children: [
                    AppGaps.vGap12,
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text(
                          '回复:${controller.clickPullName.value}',
                          style: TextStyle(
                            color: const Color(0xFF495061),
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        const Spacer(),
                        Container(
                          padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 7.w, bottom: 7.w),
                          decoration: BoxDecoration(
                            color: const Color(0xFF5C6DFF),
                            borderRadius: BorderRadius.circular(60.r),
                          ),
                          child: Text(
                            '提交',
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.w400,
                              fontSize: 14.sp,
                            ),
                          ),
                        ).clickOnce(controller.evaluateInfoCommentDialog)
                      ],
                    ),
                    AppGaps.vGap10,
                    FocusCustomEvaluateInputPage(
                      isShowTitle: false,
                      text: controller.userAddCommentDialog,
                      hintText: '请回答问题',
                      onCallBack: (value) {
                        controller.userAddCommentDialog = value;
                      },
                      isRequired: false,
                      isShowDivider: false,
                      focusNode: controller.focusNode,
                    ),
                  ],
                ),
              )), // 栏
        )));
  }

  Widget _topWidget() {
    return Scaffold(
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.only(top: 12.w, right: 16.w, left: 16.w, bottom: 129.w),
        child: Column(
          children: [
            _getSearchEnterWidget(),
            AppGaps.vGap10,
            Row(
              children: [
                AppGaps.hGap16,
                Container(
                  padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 1.w, bottom: 1.w),
                  decoration: BoxDecoration(
                    color: const Color(0x1A5C6DFF),
                    borderRadius: BorderRadius.circular(60.r),
                  ),
                  child: Text(
                    '全部(${controller.list?.length ?? ''})',
                    style: TextStyle(
                      color: controller.who ? const Color(0xFF5C6DFF) : const Color(0xFFA5A9C8),
                      fontWeight: FontWeight.w400,
                      fontSize: 12.sp,
                    ),
                  ),
                ).clickOnce(() {
                  //全部的界面 请求全部接口
                  controller.allDateOrMeDate(true);
                }),
                AppGaps.hGap10,
                Container(
                  padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 1.w, bottom: 1.w),
                  decoration: BoxDecoration(
                    color: const Color(0x1A5C6DFF),
                    borderRadius: BorderRadius.circular(60.r),
                  ),
                  child: Text(
                    '我的(${controller.listMe?.length ?? ''})',
                    style: TextStyle(
                      color: controller.who ? const Color(0xFFA5A9C8) : const Color(0xFF5C6DFF),
                      fontWeight: FontWeight.w400,
                      fontSize: 12.sp,
                    ),
                  ),
                ).clickOnce(() {
                  //我的界面 请求我的接口
                  controller.allDateOrMeDate(false);
                }),
              ],
            ),
            Expanded(
                child: CustomEasyRefreshList<EducationCommentListEntity>(
              key: controller.globalKey,
              enableLoad: false,
              // enableRefresh: false,
              dataLoad: controller.who ? controller.listCommentAll : controller.listCommentMe,
              itemBuilder: (item, index) {
                return itemWidget(item);
              },
              separated: Container(
                color: const Color(0xFFA5A9C8).withOpacity(0.2),
                height: 1.w,
              ),
            )),
          ],
        ),
      ), // 右侧筛选
      bottomSheet: Container(
        height: 129.w,
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: const Color(0xffBCBCC5).withOpacity(0.2), // 阴影颜色
              offset: const Offset(0, -2), // 阴影的偏移量，向上偏移，即上方阴影
              blurRadius: 4.0, // 阴影的模糊半径
              spreadRadius: 1.0, // 阴影的扩散半径
            ),
          ],
        ),
        child: Column(
          children: [
            AppGaps.vGap12,
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '发表问答',
                  style: TextStyle(
                    color: const Color(0xFF495061),
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                const Spacer(),
                Container(
                  padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 7.w, bottom: 7.w),
                  decoration: BoxDecoration(
                    color: const Color(0xFF5C6DFF),
                    borderRadius: BorderRadius.circular(60.r),
                  ),
                  child: Text(
                    '提交',
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w400,
                      fontSize: 14.sp,
                    ),
                  ),
                ).clickOnce(controller.evaluateInfo)
              ],
            ),
            AppGaps.vGap10,
            CustomEvaluateInputPage(
              isShowTitle: false,
              text: controller.userAddComment,
              hintText: '请输入内容',
              onCallBack: (value) {
                controller.userAddComment = value;
              },
              isRequired: false,
              isShowDivider: false,
            ),
          ],
        ),
      ), // 栏
    );
  }
}
